#include "TabSidebar.h"
#include "TabDrawer.h"
#include "DockTab.h"
#include <QVBoxLayout>

TabSidebar::TabSidebar(Qt::Edge edge, QWidget* parent)
    : QWidget(parent)
    , m_edge(edge)
    , m_layout(nullptr)
    , m_activeDrawer(nullptr)
{
    setupSidebar();
}

TabSidebar::~TabSidebar()
{
    clear();
}

void TabSidebar::setupSidebar()
{
    // 创建垂直布局
    m_layout = new QVBoxLayout(this);
    m_layout->setContentsMargins(0, 0, 0, 0);
    m_layout->setSpacing(1);
    
    // 设置固定宽度
    setFixedWidth(SIDEBAR_WIDTH);
    
    // 样式由QSS控制
    
    // 添加拉伸项到底部
    m_layout->addStretch();
}

void TabSidebar::setEdge(Qt::Edge edge)
{
    if (m_edge != edge)
    {
        m_edge = edge;
        updateLayout();
    }
}

// === 抽屉管理 ===

TabDrawer* TabSidebar::addDrawer(const QString& title)
{
    TabDrawer* drawer = new TabDrawer(title, this);
    
    // 移除拉伸项
    QLayoutItem* stretch = m_layout->takeAt(m_layout->count() - 1);
    
    // 添加抽屉
    m_layout->addWidget(drawer);
    
    // 重新添加拉伸项
    if (stretch) {
        m_layout->addItem(stretch);
    }
    
    m_drawers.append(drawer);
    
    // 连接信号
    connect(drawer, &TabDrawer::toggled, this, &TabSidebar::onDrawerToggled);
    
    emit drawerAdded(drawer);
    return drawer;
}

TabDrawer* TabSidebar::addDrawer(DockTab* tab) {
    if (!tab) return nullptr;
    
    TabDrawer* drawer = new TabDrawer(tab, this);
    
    // 移除拉伸项
    QLayoutItem* stretch = m_layout->takeAt(m_layout->count() - 1);
    
    // 添加抽屉
    m_layout->addWidget(drawer);
    
    // 重新添加拉伸项
    if (stretch) {
        m_layout->addItem(stretch);
    }
    
    m_drawers.append(drawer);
    
    // 连接信号
    connect(drawer, &TabDrawer::toggled, this, &TabSidebar::onDrawerToggled);
    
    emit drawerAdded(drawer);
    return drawer;
}

void TabSidebar::removeDrawer(TabDrawer* drawer) {
    if (!drawer) return;
    
    int index = m_drawers.indexOf(drawer);
    if (index >= 0) {
        m_drawers.removeAt(index);
        
        // 如果是活动抽屉，清除引用
        if (m_activeDrawer == drawer) {
            m_activeDrawer = nullptr;
        }
        
        // 从布局中移除
        m_layout->removeWidget(drawer);
        drawer->setParent(nullptr);
        
        emit drawerRemoved(drawer);
    }
}

void TabSidebar::clear() {
    while (!m_drawers.isEmpty()) {
        TabDrawer* drawer = m_drawers.takeLast();
        m_layout->removeWidget(drawer);
        drawer->deleteLater();
    }
    m_activeDrawer = nullptr;
}

// === 查询 ===

TabDrawer* TabSidebar::drawerAt(int index) const {
    if (index >= 0 && index < m_drawers.size()) {
        return m_drawers[index];
    }
    return nullptr;
}

// === 状态管理 ===

void TabSidebar::setActiveDrawer(TabDrawer* drawer) {
    if (m_activeDrawer == drawer) return;
    
    // 折叠之前的活动抽屉
    if (m_activeDrawer) {
        m_activeDrawer->setExpanded(false);
    }
    
    m_activeDrawer = drawer;
    
    // 展开新的活动抽屉
    if (m_activeDrawer) {
        m_activeDrawer->setExpanded(true);
    }
    
    emit activeDrawerChanged(drawer);
}

// === Qt重写 ===

QSize TabSidebar::sizeHint() const {
    return QSize(SIDEBAR_WIDTH, 400);
}

QSize TabSidebar::minimumSizeHint() const {
    return QSize(COLLAPSED_WIDTH, 100);
}

// === 槽函数 ===

void TabSidebar::onDrawerToggled(bool expanded) {
    TabDrawer* drawer = qobject_cast<TabDrawer*>(sender());
    if (!drawer) return;
    
    if (expanded) {
        // 如果展开了这个抽屉，它就成为活动抽屉
        setActiveDrawer(drawer);
    } else {
        // 如果折叠的是活动抽屉，清除活动状态
        if (m_activeDrawer == drawer) {
            m_activeDrawer = nullptr;
            emit activeDrawerChanged(nullptr);
        }
    }
}

// === 私有方法 ===

void TabSidebar::updateLayout() {
    // 根据边缘位置更新样式
    QString borderStyle;
    switch (m_edge) {
        case Qt::LeftEdge:
            borderStyle = "border-right: 1px solid #555555;";
            break;
        case Qt::RightEdge:
            borderStyle = "border-left: 1px solid #555555;";
            break;
        case Qt::TopEdge:
        case Qt::BottomEdge:
            borderStyle = "border-top: 1px solid #555555; border-bottom: 1px solid #555555;";
            break;
    }
    
    // 边框方向交由QSS按objectName或class控制，这里不再内联
}
